package com.powernode.controller;

import com.github.pagehelper.PageInfo;
import com.powernode.common.Constants;
import com.powernode.common.Result;
import com.powernode.domain.Notice;
import com.powernode.dto.NoticeDto;
import com.powernode.service.NoticeService;
import com.powernode.utils.MD5Utils;
import com.powernode.utils.WebMvcUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 内容: 完成公告的CRUD
 * 作者:
 */
@RestController
@RequestMapping("notice")
public class NoticeController {
    @Resource
    private NoticeService noticeService;
    /**
     * 分页查询公告数据
     */
    @GetMapping("list.do")
    public Result list(NoticeDto noticeDto){
        PageInfo<Notice> pageInfo=noticeService.queryNoticeForPage(noticeDto);
        return  new Result(0,"查询成功",pageInfo.getTotal(),pageInfo.getList());
    }

    /**
     * 添加公告
     */
    @PostMapping("addNotice.do")
    public Result addNotice(Notice notice){
        try{
            //设置创建时间
            notice.setCreateTime(new Date());
            //保存
            this.noticeService.insertSelective(notice);
            return Result.ADD_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return Result.ADD_ERROR;
        }
    }

    /**
     * 修改公告
     */
    @PostMapping("updateNotice.do")
    public Result updateNotice(Notice notice){
        try{
            //保存
            this.noticeService.updateByPrimaryKeySelective(notice);
            return Result.UPDATE_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return Result.UPDATE_ERROR;
        }
    }

    /**
     * 删除
     */
    @PostMapping("deleteNoticeById.do")
    public Result deleteNoticeById(Integer id){
        try{
            this.noticeService.deleteByPrimaryKey(id);
            return Result.DEL_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return Result.DEL_ERROR;
        }
    }
}
